library(ltm)
library(dplyr)
library(foreign)
library(readstata13)
library(haven)


#dataset includes black respondents that only were in the black or white experimental conditions 

load("contribution_experiment_pre_manipulation.rdata")

###experimental mturks###
#black group cond#
contexperiment$suppb <- contexperiment$q351_8
contexperiment$contb <- contexperiment$q353_4
#white group, project vote#
contexperiment$suppw <- contexperiment$q359_8
contexperiment$contw <- contexperiment$q361_4


#condition var indicator#
contexperiment$conditionvar <- NA
contexperiment$conditionvar[contexperiment$suppb >= 0] <- 1
contexperiment$conditionvar[contexperiment$suppw >= 0] <- 2

contexperiment$conditionexperiment[contexperiment$conditionvar == 0] <- "White Organization"
contexperiment$conditionexperiment[contexperiment$conditionvar == 1] <- "Black Organization"

##contribution dummy##
contexperiment$contbd <- contexperiment$contb
contexperiment$contbd[contexperiment$contbd >= 1] <- 1
contexperiment$suppbd <- contexperiment$suppb
contexperiment$suppbd[contexperiment$suppbd >= 1] <- 1


###attention check###
contexperiment$att_check <- contexperiment$attcheck
table(contexperiment$att_check)
#only keep those with a 1#
contexperiment<-subset(contexperiment, att_check ==1)
###
###ensure analysis only uses people who answered support question##
blackorgresp<-subset(contexperiment, suppb >= 0)
whiteorgresp<-subset(contexperiment, suppw >= 0)

#check summary of groups
summary(blackorgresp$contb)
summary(blackorgresp$suppb)

summary(whiteorgresp$contw)
summary(whiteorgresp$suppw)
###create combined support and pol#####
contexperiment$supp <- c()
contexperiment$supp<-ifelse(contexperiment$suppb >= 0, contexperiment$suppb, 
                           ifelse(contexperiment$suppw >=0, contexperiment$suppw, NA))

contexperiment$contibution <- contexperiment$contb
contexperiment$contribution<-ifelse(contexperiment$contw >= 0, contexperiment$contw, 
                                   ifelse(contexperiment$contl >=0, contexperiment$contl, contexperiment$contribution))

contexperiment$org <- "White"
contexperiment$org[contexperiment$suppb >= 0] <- "Black"

table(contexperiment$org)

###group consciousness measures####
#racial group importance#
contexperiment$ridimp<- c()
contexperiment$ridimp<-ifelse(contexperiment$q207 >= 0, contexperiment$q207, 
                             ifelse(contexperiment$q215 >=0, contexperiment$q215, NA))
contexperiment$ridimp<-(contexperiment$ridimp - 6) / -4 

##How important is being Black or African-American to your self-image##
contexperiment$selfimage <- c()
contexperiment$selfimage<-ifelse(contexperiment$q209 >= 0, contexperiment$q209, 
                                ifelse(contexperiment$q217 >=0, contexperiment$q217, NA))
contexperiment$selfimage<-(contexperiment$selfimage - 6) / -4 

#How much is being Black an important part of how you see yourself?#
contexperiment$impself <- c()
contexperiment$impself<-ifelse(contexperiment$identity1 >= 0, contexperiment$identity1, 
                              ifelse(contexperiment$identitymulti1 >=0, contexperiment$identitymulti1, NA))
contexperiment$impself<-(contexperiment$impself - 5) / -4 

###Does R think about being Black lot/fairly often/once in a while/or hardly ever??##
##cant find this just gonna use common fate measure##
contexperiment$blackfate[contexperiment$blackfate == 4] <- 0
contexperiment$blackfate[contexperiment$blackfate == 3] <- .33
contexperiment$blackfate[contexperiment$blackfate == 2] <- .66

##What does R say is more important to R -- being 
#Black/being American, or are both equally important?
contexperiment$autonomy[contexperiment$autonomy == 1 | contexperiment$ramulti == 1] <- 1
contexperiment$autonomy[contexperiment$autonomy == 2 | contexperiment$ramulti == 2] <- .5
contexperiment$autonomy[contexperiment$autonomy == 3 | contexperiment$ramulti == 3] <- 0

##feeling therm black americans##
contexperiment$ftblack <- contexperiment$ftblack_1 / 100

##how close do you feel to blacks##
contexperiment$close<-contexperiment$closeness_2
contexperiment$close[contexperiment$close == 1] <-  0
contexperiment$close[contexperiment$close == 2] <-  0.33
contexperiment$close[contexperiment$close == 3] <-  0.66
contexperiment$close[contexperiment$close == 4] <-  1

##how much discrimination against Black Americans##
contexperiment$discrim<-contexperiment$discrim_1
contexperiment$discrim[contexperiment$discrim == 1] <-  0
contexperiment$discrim[contexperiment$discrim == 2] <-  0.33
contexperiment$discrim[contexperiment$discrim == 3] <-  0.66
contexperiment$discrim[contexperiment$discrim == 4] <-  1

##rr##
contexperiment$rrdiscrim<-contexperiment$rr4
contexperiment$rrdiscrim[contexperiment$rrdiscrim == 5] <-  0
contexperiment$rrdiscrim[contexperiment$rrdiscrim == 4] <-  0.75
contexperiment$rrdiscrim[contexperiment$rrdiscrim == 3] <-  0.5
contexperiment$rrdiscrim[contexperiment$rrdiscrim == 2] <-  0.25

###If Black Americans don't do well in life, it is because of Race#
contexperiment$keptback <- c()
contexperiment$keptback<-ifelse(contexperiment$efficacy >= 0, contexperiment$efficacy, 
                               ifelse(contexperiment$efficacymulti >=0, contexperiment$efficiacymulti, NA))
contexperiment$keptback[contexperiment$keptback == 2] <-  0

###power black americans hold in american politics##
contexperiment$power <- c()
contexperiment$power<-ifelse(contexperiment$influence >= 0, contexperiment$influence, 
                            ifelse(contexperiment$rimulti >=0, contexperiment$rimulti, NA))
contexperiment$power[contexperiment$power == 1] <-  0
contexperiment$power[contexperiment$power == 2] <-  0.5
contexperiment$power[contexperiment$power == 3] <-  1

###It is important for people to work together to 
##improve the position of their racial or ethnic group#
contexperiment$collective[contexperiment$collective == 1] <-0
contexperiment$collective[contexperiment$collective == 2] <-0.25
contexperiment$collective[contexperiment$collective == 3] <-0.5
contexperiment$collective[contexperiment$collective == 4] <-0.75
contexperiment$collective[contexperiment$collective == 5] <-1

#if enough black americans vote they can make a difference#
contexperiment$votediff <- c()
contexperiment$votediff<-ifelse(contexperiment$efficacy1 >= 0, contexperiment$efficacy1, 
                               ifelse(contexperiment$efficacymulti >=0, contexperiment$efficacymulti, NA))
contexperiment$votediff[contexperiment$votediff == 1] <-  0
contexperiment$votediff[contexperiment$votediff == 2] <-  0.25
contexperiment$votediff[contexperiment$votediff == 3] <-  0.5
contexperiment$votediff[contexperiment$votediff == 4] <-  0.75
contexperiment$votediff[contexperiment$votediff == 5] <-  1

###To have power and improve their position in the United
#States Black people should be more active in black organizations
contexperiment$blackorg <- c()
contexperiment$blackorg<-ifelse(contexperiment$collectiveaction_1 >= 0, contexperiment$collectiveaction_1, 
                               ifelse(contexperiment$collectivemultirace_1 >=0, contexperiment$collectivemultirace_1, NA))
contexperiment$blackorg[contexperiment$blackorg == 1] <-  0
contexperiment$blackorg[contexperiment$blackorg == 2] <-  0.25
contexperiment$blackorg[contexperiment$blackorg == 3] <-  0.5
contexperiment$blackorg[contexperiment$blackorg == 4] <-  0.75
contexperiment$blackorg[contexperiment$blackorg == 5] <-  1

###Do you think that the movement for 
#Black rights has affected you personally?
contexperiment$blackrights[contexperiment$blackrights == 2] <- 0

##To have power and improve their position in the United States 
#each Black person should work hard to 
#improve his or her own personal situation
contexperiment$workhardindv <- c()
contexperiment$workhardindv<-ifelse(contexperiment$collectiveaction_2 >= 0, contexperiment$collectiveaction_2, 
                                   ifelse(contexperiment$collectivemultirace_2 >=0, contexperiment$collectivemultirace_2, NA))
contexperiment$workhardindv[contexperiment$workhardindv == 2] <-  0.75
contexperiment$workhardindv[contexperiment$workhardindv == 3] <-  0.5
contexperiment$workhardindv[contexperiment$workhardindv == 4] <-  0.25
contexperiment$workhardindv[contexperiment$workhardindv == 5] <-  0

#linkedfate#
contexperiment$linkfate <- c()
contexperiment$linkfate[contexperiment$lf1 == 1 & contexperiment$lf2 == 3] <- 1
contexperiment$linkfate[contexperiment$lf1 == 1 & contexperiment$lf2 == 2] <- .66
contexperiment$linkfate[contexperiment$lf1 == 1 & contexperiment$lf2 == 1] <- .33
contexperiment$linkfate[contexperiment$lf1 == 2] <- 0

contexperiment$linkfate[contexperiment$lfmulti1 == 1 & contexperiment$lfmulti2 == 3] <- 1
contexperiment$linkfate[contexperiment$lfmulti1 == 1 & contexperiment$lfmulti2 == 2] <- .66
contexperiment$linkfate[contexperiment$lfmulti1 == 1 & contexperiment$lfmulti2 == 1] <- .33
contexperiment$linkfate[contexperiment$lfmulti1 == 2] <- 0




###rgc mean###
contexperiment$rgcmean <- (rowMeans(contexperiment[c("ridimp", "selfimage", "impself", 
                                                   "blackfate", "autonomy", "ftblack",
                                                   "close","discrim","rrdiscrim",
                                                   "keptback", "power", "collective",
                                                   "votediff", "blackorg", "blackrights", 
                                                   "workhardindv", "linkfate")], na.rm=TRUE))
summary(contexperiment$rgcmean)
###cronbach's alpha###
library(ltm)
myvars <- c("ridimp", "selfimage", "impself", 
            "blackfate", "autonomy", "ftblack",
            "close","discrim","rrdiscrim",
            "keptback", "power", "collective",
            "votediff", "blackorg", "blackrights", 
            "workhardindv", "linkfate")

ndexp <- contexperiment[myvars]

cronbach.alpha(ndexp, na.rm = TRUE)

#descriptive statistics of experiment####
contexperiment$female[contexperiment$gender == 1] <- 0
contexperiment$female[contexperiment$gender == 3] <- 0
contexperiment$female[contexperiment$gender == 2] <- 1
mean(contexperiment$female)

contexperiment$democrat[contexperiment$pid == 3 & contexperiment$pidind == 2] <- 0
contexperiment$democrat[contexperiment$pid == 3 & contexperiment$pidind == 3] <- 0
contexperiment$democrat[contexperiment$pid == 2] <- 0
contexperiment$democrat[contexperiment$pid == 3 & contexperiment$pidind == 1] <- 1
contexperiment$democrat[contexperiment$pid == 1] <- 1
contexperiment$democrat[is.na(contexperiment$democrat)] <- 0
mean(contexperiment$democrat)

median(contexperiment$ideo)

median(contexperiment$y_born, na.rm = TRUE)

median(contexperiment$edu, na.rm = TRUE)
median(contexperiment$inc, na.rm = TRUE)

contribution_final <- contexperiment %>%
  dplyr::select(multirace, gender, hisp, race, multirace, y_born, edu, inc, state, pid, pidind, piddem,
                pidrep, ideo, polint, newsint, newsfreq, pk1, pk2, pk3, pk4, pk5, att_check, q178, q179,
                blackrights, blackfate, closest, collective, efficacy, influence, identity1, identity2, 
                organization, blackorganization, autonomy, mturk, numdays, totnumday, attcheck, black, suppb,
                contb, suppw, contw, conditionvar, org, supp, contribution, ridimp, selfimage, impself, ftblack,
                close, discrim, rrdiscrim, keptback, power, votediff, blackorg, workhardindv, linkfate, female,
                conditionexperiment, rgcmean)
df<-contribution_final
save(df, file = "df.rdata")
